package com.yenlien.traceyun.control.service.test;

import com.yenlien.traceyun.control.BaseTest;
import org.junit.Test;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.test.annotation.Rollback;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-11-20
 * @description:
 **/
public class DataTrans extends BaseTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    @Rollback(false)
    public void trans(){
       SqlRowSet rowSet   = jdbcTemplate.queryForRowSet("select * from t_model_crop_copy1 ");
        int code=0;
       while(rowSet.next()){
           String name = rowSet.getString("name");
           String otherName = rowSet.getString("other_name");
//           String code = rowSet.getString("code");
           String img = rowSet.getString("img");
           String id = rowSet.getString("id");
           String remarks = rowSet.getString("remarks");
           System.out.println(name);
           String oneId = rowSet.getString("one_id");
           String twoId = rowSet.getString("two_id");
           String cateogryid=null;
           if(StringUtils.isNotBlank(twoId)){
               cateogryid=twoId;
           }else{
               cateogryid=oneId;
           }
           insert(id, name, otherName, (++code)+"",img,remarks,cateogryid);
       }


//        SqlRowSet classrow   = jdbcTemplate.queryForRowSet("select * from t_model_classify_copy1 ");
//
//        int code=0;
//        while(classrow.next()){
//            String id = classrow.getString("id");
//           String name = classrow.getString("name");
//           String sort = classrow.getString("sort");
//           String parentId = classrow.getString("parent_id");
////           String code = classrow.getString("code");
//           String displayImg = classrow.getString("display_img");
//           String type = classrow.getString("type");
//           if(sort==null || "".equals(sort)){
//              sort="0";
//           }
//
//            System.out.println("id:"+id+" name:"+name+" sort:"+sort+" parent:"+parentId+" code:"+code+" displayImg:"+displayImg+" type:"+type);
//            insertCategory(id, name, sort, parentId, (++code)+"", displayImg, type);
//       }


    }



    public void insert(String id,String name ,String otherName,String code,String img, String remarks,String categoryId){
        jdbcTemplate.update("insert into  t_model_crop(id,name,other_name,code,img,remarks,category_id) values(?,?,?,?,?,?,?)",id,name,otherName,code,img,remarks,categoryId);
    }

    public void insertCategory(String id,String name ,String sort_num,String parent_id,String code, String displayImg,String type){
        jdbcTemplate.update("insert into  t_model_category(id,name,sort_num,parent_id,code,display_img,type) values(?,?,?,?,?,?,?)",id,name,sort_num,parent_id,code,displayImg,type);
    }


}
